﻿Imports CapaLogica.CL.Blog
Imports CapaLogica.CL.Entrada
Imports System.Globalization
Imports CapaLogica.CL.ComentarioEntrada
Imports System.Threading

Public Class ConsultarEntrada
    Inherits System.Web.UI.Page

    Private blogValido As Boolean

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("id") Is Nothing Then
            Response.Redirect("Login.aspx")
        End If

        If Session("rol") = "1" Then
            Response.Redirect("Inicio.aspx")
        End If
    End Sub

    Public Function datosBlog() As Dictionary(Of String, Object)

        Try
            Dim datosDelBlog As Dictionary(Of String, Object) = Nothing

            If Request("accion") = "MiEntrada" Then
                datosDelBlog = GestorBlog.obtenerBlogUsuario(CType(Session("id"), Integer))

            ElseIf Request("accion") = "visita" Then
                datosDelBlog = GestorBlog.obtenerBlogUsuario(CType(Request("idUsuario"), Integer))
                Session.Add("idUsuario", Request("idUsuario"))

            End If

            Session.Add("idEntrada", Request("idEntrada"))

            Return datosDelBlog
        Catch ex As Exception
            Seguridad.redirigirPorError("Ocurrio un error a la hora de cargar los datos del blog", Response)
        End Try


    End Function



    Public Function comentarios() As List(Of Dictionary(Of String, Object))
        Try
            Dim datosComentarios As List(Of Dictionary(Of String, Object)) = GestorEntrada.listarComentariosEntrada(CType(Session("idEntrada"), Integer))

            Return datosComentarios
        Catch ex As Exception
            Seguridad.redirigirPorError("Ocurrio un error a la hora de anular la expulsión", Response)
        End Try


    End Function


    Public Function get_htmlEntrada() As String

        Try
            Dim cultura As New CultureInfo("es-CR")
            Dim entrada As Dictionary(Of String, Object) = GestorEntrada.consultarEntrada(CType(Session("idEntrada"), Integer))
            Dim html As String = ""

            txtEntrada.Text = CType(entrada("comentario"), String)
            txtTitulo.Text = CType(entrada("titulo"), String)

            html += "<div id='ent" & CType(entrada("id"), String) & "' class='entrada'>" &
                        "<h3>" & CType(entrada("fecha"), DateTime).ToString("D", cultura) & "</h3>" &
                        "<div class='contenido'>" &
                            "<div class='titulo'><h2>" & CType(entrada("titulo"), String) & "</h2></div>" &
                            "<div class='texto'>" &
                                "<span>" & CType(entrada("comentario"), String) & "</span>" &
                            "</div>" &
                        "</div>" &
                        "<br>"

            html += "</div>"


            Return html
        Catch ex As Exception
            Seguridad.redirigirPorError("Ocurrio un error a la hora de generar la entrada", Response)
        End Try



    End Function



    Public Function validarBlog(ByRef datosBlog As Dictionary(Of String, Object)) As String

        Try
            Dim estado As Boolean = CType(datosBlog("estado"), Boolean)
            Dim bloqueado As Boolean = CType(datosBlog("bloqueado"), Boolean)



            If bloqueado = True Then
                blogValido = False
                Return "Blog Bloqueado"

            ElseIf estado = False Then
                blogValido = False
                Return "Blog Desactivado"

            Else
                blogValido = True
                Return ""
            End If
        Catch ex As Exception
            Seguridad.redirigirPorError("Ocurrio un error a la hora de validar el blog", Response)
        End Try




    End Function


    Public Function get_htmlComentarios() As String
        Dim html As String = ""
        Dim datosComentarios As List(Of Dictionary(Of String, Object)) = GestorEntrada.listarComentariosEntrada(CType(Session("idEntrada"), Integer))
        Try

            For Each comentario As Dictionary(Of String, Object) In datosComentarios

                html += "<div id='aporte_" & CType(comentario("id"), Integer) & "' class='content_foro'>" &
                         "<div style='width:600px;height: 25px'>" &
                         "</div>" &
                          "<div class='lbox_n2' id='div_aporte_" & CType(comentario("id"), Integer) & "'><!--Info del usuario-->" &
                           "<div class='info_user_n2'>" &
                           "	<div align='center'>" &
                           "		<img class='fotos_user_n1' src='images/user_default.jpg' alt=''>" &
                           "			<p>" & CType(comentario("nombreUsuario"), String) & "</p>" &
                           "			</div>" &
                           "</div><!--Encabezado-->" &
                           "<div class='encab_foro'>" &
                           "	<p style='text-align:right'>" & CType(comentario("fecha"), String) & "</p>" &
                           "</div>" &
                           "	<p id='des_aporte_" & CType(comentario("id"), Integer) & "'>" & CType(comentario("comentario"), String) & "</p><!--Footer-->" &
                           "<div align='right'>" &
                           "</div>" &
                          "</div>" &
                        "</div>"
            Next

            Return html
        Catch ex As Exception
            Seguridad.redirigirPorError("Ocurrio un error a la hora de obtener los comentarios", Response)
        End Try
    End Function


    Public Function habilitarBotonEscrituraEntrada() As String
        Try
            If Request("accion") = "MiEntrada" Then
                If blogValido = True Then
                    Return "<input id='btnEscribirEntrada' type='button' value='Modificar Entrada' onclick='habilitarEscrituraEntrada()'/>"

                End If
            Else
                Return ""
            End If
        Catch ex As Exception
            Seguridad.redirigirPorError("Ocurrio un error.", Response)
        End Try


    End Function

    Protected Sub btnCrearEntrada_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCrearEntrada.Click
        Try
            GestorComentarioEntrada.crearComentario(txtComentario.Text, CType(Session("idEntrada"), Integer))
            Response.Redirect("ConsultarEntrada.aspx?accion=MiEntrada&idEntrada=" & Session("idEntrada"))

        Catch exth As ThreadAbortException
            Response.Redirect("ConsultarEntrada.aspx?accion=MiEntrada&idEntrada=" & Session("idEntrada"))
        Catch ex As Exception
            Seguridad.redirigirPorError("Ha habido un error al crear el comentario.", Response)
        End Try


    End Sub

    Protected Sub btnRegistrarEntrada_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRegistrarEntrada.Click
        Try
            GestorEntrada.modificarEntrada(CType(Session("idEntrada"), Integer), txtTitulo.Text, txtEntrada.Text)
            Response.Redirect("ConsultarEntrada.aspx?accion=MiEntrada&idEntrada=" & Session("idEntrada"))
        Catch exth As ThreadAbortException
            Response.Redirect("ConsultarEntrada.aspx?accion=MiEntrada&idEntrada=" & Session("idEntrada"))
        Catch ex As Exception
            Seguridad.redirigirPorError("Ha habido un error al modificar la entrada.", Response)
        End Try



    End Sub
End Class